package com.lej.app_interact.search

import androidx.lifecycle.MutableLiveData
import com.lej.app_interact.InteractApiService
import com.lej.core.base.BaseModel
import com.lej.core.base.BaseViewModel
import com.lej.core.utils.extention.toast
import com.lej.dataservice.club.bean.interact.CreatorSearchInfo
import com.lej.dataservice.club.bean.interact.CreatorSearchReq
import com.lej.dataservice.club.http.base.toBody
import com.lej.dataservice.club.http.checkData


class InteractSearchViewModel : BaseViewModel<BaseModel>() {
    val searchName: MutableLiveData<String> = MutableLiveData("")
    val recodeVisible: MutableLiveData<Boolean> = MutableLiveData(false)
    val emptyVisible: MutableLiveData<Boolean> = MutableLiveData(false)
    val visitList: MutableLiveData<List<CreatorSearchInfo>> = MutableLiveData()

//    suspend fun getLocalRecordData(): ArrayList<CreatorSearchInfo>? {
//        val recodeListStr = MMKVUtils.getInstance().getString(RECODE_DATA)
//        return recodeListStr.fromJson<ArrayList<CreatorSearchInfo>>()
//    }

    suspend fun getNetRecodeData(): List<CreatorSearchInfo>? {
        kotlin.runCatching {
            visitList.value = InteractApiService.instance.getVisitHistory().checkData().historyList
//            val list = ArrayList<CreatorSearchInfo>()
//            var localList: ArrayList<CreatorSearchInfo>? = getLocalRecordData()
//            if (localList == null) {
//                localList = ArrayList()
//            } else {
//                localList.clear()
//            }
//            localList.addAll(list)
//            val str = localList.toJson()
//            MMKVUtils.getInstance().put(RECODE_DATA, str)
        }.onFailure {
            "请求失败，请重试".toast()
            return null
        }
        return null
    }

    suspend fun getSearchData(): List<CreatorSearchInfo>? {
        if (searchName.value.isNullOrEmpty()) {
            return null
        } else {
            kotlin.runCatching {
                val creatorReq = CreatorSearchReq(searchName.value!!)
                return InteractApiService.instance.searchCreator(creatorReq.toBody())
                    .checkData().creatorList
            }.onFailure {
                "请求失败，请重试".toast()
                return null
            }
        }
        return null
    }

}